package com.workcell.workdata.xthings.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.constant.HardwareStateEnum;
import com.workcell.workdata.xthings.entity.BaseBeltScales;
import com.workcell.workdata.xthings.entity.CodeBeltScales;
import com.workcell.workdata.xthings.entity.CodeChainnum;
import com.workcell.workdata.xthings.mapper.CodeBeltScalesMapper;
import com.workcell.workdata.xthings.service.BaseBeltScalesService;
import com.workcell.workdata.xthings.service.CodeBeltScalesService;
import com.workcell.workdata.xthings.service.factory.CodeBaseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CodeBeltScalesServiceImpl extends ServiceImpl<CodeBeltScalesMapper, CodeBeltScales> implements CodeBeltScalesService, CodeBaseService {
	
	@Resource
	private BaseBeltScalesService baseBeltScalesService;

	/**
	 * 保存二维码基本快照信息
	 *
	 * @param codeChainnum
	 * @return
	 */
	@Override
	public Boolean saveCodeBaseInfo(CodeChainnum codeChainnum) {
		// 查询硬件信息
		LambdaQueryWrapper<BaseBeltScales> baseCondition = new QueryWrapper<BaseBeltScales>().lambda()
				.eq(BaseBeltScales::getId, Long.valueOf(codeChainnum.getBaseId()));
		BaseBeltScales baseBeltScales = baseBeltScalesService.getOne(baseCondition);
		if (ObjectUtil.isEmpty(baseBeltScales)) {
			return false;
		}
		// 添加二维码快照信息
		CodeBeltScales saveBean = new CodeBeltScales();
		BeanUtil.copyProperties(baseBeltScales, saveBean, "createBy", "createTime", "updateBy", "updateTime");
		saveBean.setCodeId(codeChainnum.getCodeId());
		saveBean.setChainNum(codeChainnum.getChainNum());
		saveBean.setDeviceNo(codeChainnum.getDeviceNo());
		saveBean.setBsId(codeChainnum.getBaseId());
		saveBean.setBsName(codeChainnum.getHardwareName());
		return this.save(saveBean);
	}

	/**
	 * 获取硬件清单
	 *
	 * @return
	 */
	@Override
	public List<JSONObject> getHardwareList() {
		LambdaQueryWrapper<BaseBeltScales> lambdaQueryWrapper = Wrappers.lambdaQuery(BaseBeltScales.class);
		lambdaQueryWrapper.eq(BaseBeltScales::getState, HardwareStateEnum.INUSE.getCode());
		return baseBeltScalesService.list(lambdaQueryWrapper).stream().map(i -> {
			JSONObject resultBean = new JSONObject();
			resultBean.put("baseId", String.valueOf(i.getId()));
			resultBean.put("manufacturer", i.getManufacturer());
			resultBean.put("hardwareName", i.getName());
			return resultBean;
		}).collect(Collectors.toList());
	}

	/**
	 * 查询硬件基本信息
	 *
	 * @param baseIds
	 * @return
	 */
	@Override
	public Object getHardwareBase(String baseIds) {
		List<String> baseList = Arrays.asList(baseIds.split(","));
		LambdaQueryWrapper<BaseBeltScales> baseBeltScalesCondition = new QueryWrapper<BaseBeltScales>().lambda()
				.in(CollectionUtil.isNotEmpty(baseList), BaseBeltScales::getId, baseList);
		List<BaseBeltScales> baseBeltScalesList = baseBeltScalesService.list(baseBeltScalesCondition);
		return baseBeltScalesList;
	}

	/**
	 * 查询硬件快照信息
	 *
	 * @param codeId
	 * @return
	 */
	@Override
	public Object getHardwareInfo(String codeId) {
		return this.getById(codeId);
	}

}
